package kr.saeildamil.androidstudy_second31;

import java.util.ArrayList;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GestureOverlay extends Activity {
	WebView mWeb;
	GestureLibrary mLibrary;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gestureoverlay);
		
		mWeb=(WebView) findViewById(R.id.ch31_gestureoverlay_web);
		mWeb.setWebViewClient(new MyWebClient());
		WebSettings set=mWeb.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		mWeb.loadUrl("http://www.naver.com");
		
		mLibrary=GestureLibraries.fromRawResource(this, R.raw.gestures);
		if(mLibrary.load()==false){
			finish();
		}
		GestureOverlayView gestures=(GestureOverlayView) findViewById(R.id.ch31_gestureoverlay);
		gestures.addOnGesturePerformedListener(mListener);
	}
	
	class MyWebClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return super.shouldOverrideUrlLoading(view, url);
		}
	}
	OnGesturePerformedListener mListener=new OnGesturePerformedListener() {
		
		@Override
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			ArrayList<Prediction>predictions=mLibrary.recognize(gesture);
			
			if(predictions.size()!=0){
				Prediction prediction=predictions.get(0);
				String name=prediction.name;
				if(name.equals("kiyuk")){
					mWeb.goBack();
				}else if(name.equals("nieun")){
					mWeb.goForward();
				}else if(name.equals("digut")){
					mWeb.loadUrl("http://www.saeildamil.co.kr");
				}
			}
		}
	};
}
